/*
 * Compressed by JSA(www.xidea.org)
 */
Ext.Ajax.on("requestcomplete", function (_, A) {
    var $ = A.responseText, B = $.indexOf("#noLogin#");
    if (B != -1) location.reload()
});
var CMP_FUNCTIONS = {}, CMP_TOP;
Ext.onReady(function () {
    Ext.get("loading").update("");
    Ext.form.Field.prototype.msgTarget = "under";
    Ext.Ajax.setDefaultHeaders({VERSION: "cxAdmin"});
    _.post("admin/home", {}, function ($) {
        $ = Ext.decode($);
        APP_DICS = $.dics;
        Ext.Object.each(APP_DICS, function (_, $) {
            APP_DICS_CN[_] = {};
            Ext.each(APP_DICS[_], function ($) {
                APP_DICS_CN[_][$.itemVal] = $.itemCn
            })
        });
        DEBUG = $.debug;
        APP_USER = $.user;
        APP_BUTTONS = {};
        if (APP_USER.buttons) Ext.each(APP_USER.buttons.split(","), function ($) {
            APP_BUTTONS[$] = true
        })
    }, false);
    var $ = _.treeStore({
        startId: "index",
        entityClass: SysModel,
        wheres: APP_USER.isAdmin ? [["modelType", "=", "1"]] : [["modelType", "=", "1"], ["id", "in", APP_USER.models]],
        orders: [["modelNum", "asc"]],
        mapper: "id,modelName,modelParent,modelNum,modelMain,modelJs,iconCls,modelParent,modelInfo,modelName",
        slisteners: {
            load: function () {
                var A = mainCenter(), B = mainNorth(), C = mainWest();
                _({
                    layout: "border", xtype: "viewport", items: [A, B, C], listeners: {
                        afterrender: function () {
                            var A = _("main-top"), D = _("main-west"), B = _("main-context"), E = [];
                            $.getRoot().eachChild(function ($) {
                                var _ = {
                                    minWidth: 90,
                                    id: Ext.id(),
                                    iconCls: $.get("iconCls"),
                                    pid: $.get("id"),
                                    baseCls: "me-tool",
                                    text: $.get("text"),
                                    handler: function () {
                                        var C = D.getStore().getRoot();
                                        CMP_TOP = _.id;
                                        C.removeAll();
                                        $.eachChild(function (A) {
                                            C.appendChild({
                                                topId: _.id,
                                                pid: $.get("id"),
                                                id: A.get("id"),
                                                modelName: A.get("modelName"),
                                                modelJs: A.get("modelJs"),
                                                modelMain: A.get("modelMain"),
                                                iconCls: A.get("iconCls"),
                                                text: A.get("text"),
                                                leaf: A.get("leaf"),
                                                minWidth: 90,
                                                expanded: true,
                                                children: A.get("children")
                                            })
                                        });
                                        C.cascadeBy(function ($) {
                                            addInfo($)
                                        });
                                        var A = B.getActiveTab(), E = A.modelJs;
                                        if (E) {
                                            var F = D.getStore().findNode("modelJs", E);
                                            if (F) D.updateSelection(F)
                                        }
                                        Ext.util.Cookies.set("activityModelTop", _.id);
                                        syncMenuColor(_)
                                    }
                                };
                                E.push(_)
                            });
                            var F, C;
                            Ext.each(E, function (_) {
                                var $ = A.insert(2, _);
                                F = $;
                                if (!C) C = $
                            }, this, true);
                            if (C) C.on("afterrender", function ($) {
                                initMenuStart(D, $, F)
                            });
                            this.updateLayout()
                        }
                    }
                })
            }
        }
    })
});

function mainCenter() {
    var $ = {};
    $.title = "\u4e3b\u9875";
    return {
        region: "center",
        bodyStyle: {backgroundColor: "#FFF"},
        region: "center",
        xtype: "tabpanel",
        plugins: Ext.create("Ext.ux.TabCloseMenu", {}),
        id: "main-context",
        items: [$],
        listeners: {
            tabchange: function (A, D, C) {
                var E = _("main-west"), F = D.topId;
                if (F) {
                    var B = _(F);
                    if (F != CMP_TOP) B.handler();
                    var $ = E.getStore().findNode("modelJs", D.modelJs);
                    E.updateSelection($, E.getStore().findNode("modelJs", C.modelJs))
                } else {
                    $ = E.getItem(E.getStore().findNode("modelJs", C.modelJs));
                    if ($) $.setSelected(false)
                }
                if (_(D.id)) _.cascadeReset(D)
            }
        }
    }
}

function mainWest() {
    return {
        id: "mainWest_parent",
        region: "west",
        layout: {type: "vbox", align: "stretch"},
        scrollable: "y",
        bodyStyle: {backgroundColor: "#333744"},
        width: 220,
        items: {
            id: "main-west", highlightPath: true, xtype: "treelist", listeners: {
                click: function (_, $) {
                    if ($.isLeaf()) addModel($)
                }
            }, store: Ext.create("Ext.data.TreeStore", {root: {}})
        }
    }
}

function mainNorth() {
    return {
        id: "main-top",
        xtype: "toolbar",
        height: 60,
        minHeight: 60,
        region: "north",
        style: {height: 60, padding: "0px", backgroundColor: "#35BAF6"},
        defaults: {width: 40},
        items: [{
            baseCls: "me-tool-logo",
            text: "<img height=25 class=\"me-tool-logo-img\"  src=\"cx-files/code/imgs/logo3.png\">",
            width: 220
        }, {
            baseCls: "me-tool", iconCls: "fa-navicon me-white", handler: function () {
                var $ = _("mainWest_parent");
                $.isHidden() ? $.show() : $.hide()
            }
        }, "->", _.fmt("<img class=\"me-user\" src=\"{0}\">", APP_USER.userImg), {
            text: APP_USER.name,
            width: "auto",
            baseCls: "me-tool",
            cls: "me-white"
        }, {
            baseCls: "me-tool",
            iconCls: "fa-ellipsis-v me-white",
            menu: new Ext.menu.Menu({
                items: [{
                    iconCls: "fa-user",
                    text: "\u4fee\u6539\u8d44\u6599",
                    handler: function () {
                        changeUserInfo()
                    }
                }, {
                    iconCls: "fa-table", text: "\u4fee\u6539\u5bc6\u7801", handler: function () {
                        changeUserPwd()
                    }
                }, {
                    iconCls: "fa-close", text: "\u9000\u51fa\u7cfb\u7edf", handler: function () {
                        cancelLogin()
                    }
                }]
            })
        }]
    }
}

function addModel(node) {
    var path = node.get("modelJs"), main = node.get("modelMain"), app;
    if (!path || !main) return;

    function addCmp() {
        app = eval(main);
        app.title = node.get("modelName");
        app.closable = true;
        CMP_FUNCTIONS[app.id] = {main: main, title: app.title};
        app.modelJs = path;
        addMain(app, node)
    }

    if (!adaptor_exitJavascript(path)) {
        var suffix = "";
        if (DEBUG) suffix = "?dc=" + new Date().getTime();
        adaptor_implJavaScript(path + suffix, addCmp)
    } else addCmp();
    Ext.util.Cookies.set("activityModel", node.get("id"))
}

function addMain($, A) {
    var C = _("main-context");
    if ($) {
        var B = C.getComponent($.id);
        if (!B) {
            B = C.add($);
            B.topId = A.topId = A.get("topId") ? A.get("topId") : A.parentNode.get("topId")
        }
        C.setActiveItem(B);
        return B
    }
}

function addInfo(node) {
    var info = node.get("modelInfo"), text = node.get("modelName");
    if (info) {
        var result = eval(info);
        if (result) node.set("text", text + _.fmt("<span class=\"menu-info {1}\" style=\"{2}\">{0}</span>", result.info, result.cls, result.style))
    }
}

function changeUserInfo() {
    _({
        entityClass: SysUser,
        xtype: "jform",
        cascad: false,
        fields: [{text: "\u4e0a\u4f20\u5934\u50cf", xtype: "imgfield", name: "portrait"}, {name: "name", req: true}]
    }).update({
        cascad: false,
        title: "\u4fee\u6539\u8d44\u6599",
        msg: "\u8d44\u6599\u4fee\u6539\u6210\u529f,\u91cd\u65b0\u767b\u9646\u540e\u751f\u6548",
        entityId: APP_USER.uid
    })
}

function changeUserPwd() {
    _({
        xtype: "jform",
        defaults: {lw: 90},
        width: 400,
        fields: [{req: true, text: "\u539f\u5bc6\u7801", maxLength: 24, name: "pwd", inputType: "password"}, {
            req: true,
            maxLength: 24,
            minLength: 6,
            text: "\u65b0\u5bc6\u7801",
            name: "new",
            inputType: "password"
        }, {
            minLength: 6,
            maxLength: 24,
            req: true,
            text: "\u786e\u8ba4\u65b0\u5bc6\u7801",
            name: "newPwd",
            inputType: "password"
        }]
    }).save({
        btnFn: function ($) {
            if ($.get("new").val() != $.get("newPwd").val()) {
                Ext.Msg.alert("\u9519\u8bef", "\u4e24\u6b21\u5bc6\u7801\u8f93\u5165\u4e0d\u4e00\u81f4");
                return false
            }
        },
        msg: "\u5bc6\u7801\u4fee\u6539\u6210\u529f",
        url: "admin/changePwd",
        title: "\u4fee\u6539\u5bc6\u7801",
        text: "\u786e\u5b9a"
    })
}

function cancelLogin() {
    window.location.href = "admin/cancelLogin"
}

function syncMenuColor(A) {
    if (_(A.id).getEl()) {
        $("#main-top .me-tool").css("background-color", "");
        $(_(A.id).getEl().dom).css("background-color", "#00B2EE")
    }
}

function initMenuStart(B, C, D) {
    var $ = Ext.util.Cookies.get("activityModelTop"), A = Ext.util.Cookies.get("activityModel");
    if (A && $) {
        _($).handler();
        var E = B.getStore().findNode("id", A);
        if (E) B.fireEvent("click", B, E);
        syncMenuColor({id: $})
    } else D.handler()
}